package com.itheima.stream;

import java.io.UnsupportedEncodingException;

/**
 * 2025/5/9 21:00
 * 演示：编码和解码
 */
public class StringDemo {
    public static void main(String[] args) throws UnsupportedEncodingException {
//        test1();
        test2();
    }

    //编码  将文字转换成字节数组
    public static void test1() throws UnsupportedEncodingException {
        String s = "我";//3个字节 2个字节
        //获取字符串的编码
//        byte[] bytes = s.getBytes();//utf-8
        byte[] bytes = s.getBytes("GBK");//GBK
        for (byte b : bytes) {
            System.out.println(b);
        }
    }
    //解码  将字节数组转换成文字
    public static void test2() throws UnsupportedEncodingException {
        byte[] bytes = {-50,-46};//"我"--GBK---【-50，-46】----gbk---->"我"
        String data = new String(bytes,"GBK");//utf-8
        System.out.println(data);
    }

}
